package com.gdut.simple.view.activity.user;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.gdut.simple.R;
import com.gdut.simple.contract.UpdateInfoContract;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.event.RxBus;
import com.gdut.simple.event.UserEvent;
import com.gdut.simple.model.impl.UpdateIntroModelImpl;
import com.gdut.simple.model.impl.UpdateNameModelImpl;
import com.gdut.simple.model.impl.UpdateSiteModelImpl;
import com.gdut.simple.presenter.user.UpdateInfoPresenter;
import com.gdut.simple.view.basic.AbsBasicActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;

public class UpdateIntroActivity extends AbsBasicActivity implements UpdateInfoContract.View {

    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_input_content_text)
    EditText mInputText;

    private UpdateInfoContract.Presenter mPresenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_update_user_info);
        ButterKnife.bind(this);
        new UpdateInfoPresenter(this);
        initToolbar();
    }

    private void initToolbar() {
        mToolbar.setTitle("修改简介");
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }

    @Override
    protected void loadData() {
        User currentUser = BmobUser.getCurrentUser(User.class);
        if (currentUser == null || currentUser.getObjectId() == null) {
            return;
        }
        mInputText.setText(currentUser.getIntro());
        mInputText.setSelection(mInputText.getText().toString().length());
        mInputText.setHint("请输入简介");
        mInputText.setGravity(Gravity.START);
        mInputText.setMinLines(5);
        mInputText.setMaxLines(8);
        mInputText.setFilters(new InputFilter[]{
                new InputFilter.LengthFilter(256)
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_done, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_done:
                mPresenter.update(new UpdateInfoContract.UpdateStrategy(new UpdateIntroModelImpl()));
                break;
        }
        return true;
    }

    @Override
    public User getUser() {
        return BmobUser.getCurrentUser(User.class);
    }

    @Override
    public String getValue() {
        return mInputText != null ? mInputText.getText().toString() : "";
    }

    @Override
    public void next() {
        RxBus.getDefault().post(new UserEvent(true));
        finish();
    }

    @Override
    public void setPresenter(@NonNull UpdateInfoContract.Presenter presenter) {
        this.mPresenter = presenter;
    }
}
